/*
 * DetailUpdateFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import com.insigma.yanwenlong.bookkeeping.pojo.Account;
import com.insigma.yanwenlong.bookkeeping.pojo.Category;
import com.insigma.yanwenlong.bookkeeping.pojo.Member;
import com.insigma.yanwenlong.bookkeeping.pojo.vo.DetailVo;
import com.insigma.yanwenlong.bookkeeping.service.AccountService;
import com.insigma.yanwenlong.bookkeeping.service.AccountServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.CategoryService;
import com.insigma.yanwenlong.bookkeeping.service.CategoryServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.DetailService;
import com.insigma.yanwenlong.bookkeeping.service.DetailServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.MemberService;
import com.insigma.yanwenlong.bookkeeping.service.MemberServiceImpl;
import com.insigma.yanwenlong.bookkeeping.util.StringToDate;

/**
 *
 * @author  __USER__
 */
public class DetailUpdateFrame extends javax.swing.JFrame {

	private JTable tblDetail;
	private AccountService accountService = new AccountServiceImpl();
	private DetailService detailService = new DetailServiceImpl();
	private CategoryService categoryService = new CategoryServiceImpl();
	private MemberService memberService = new MemberServiceImpl();
	private DetailVo detailVo;

	/** Creates new form DetailUpdateFrame */
	public DetailUpdateFrame() {
		this.setTitle("修改账务信息");
		initComponents();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	public DetailUpdateFrame(JTable tblDetail) {
		this();
		this.tblDetail = tblDetail;
		int id = (Integer) tblDetail.getValueAt(tblDetail.getSelectedRow(), 0);
		try {
			detailVo = detailService.finDetailVoById(id);
			//初始化账户
			initAccount(detailVo.getAccountName());
			//显示交易时间
			jtfTranTime.setText(detailVo.getTranTime().toString());
			//显示发生金额
			spnDetailMoney.setValue((Double) detailVo.getHappenMoney());
			//显示类别
			initCategory(detailVo.getCategoryName());
			//显示家庭成员
			initMember(detailVo.getMemberName());
			//显示说明
			jtaNote.setText(detailVo.getNote());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	private void initMember(String memberName) {
		try {
			List<Member> members = memberService.findallMembers();
			DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobDetailMember
					.getModel();
			for (Member member : members) {
				if (member.getName().equals(memberName)) {
					dcbm.setSelectedItem(member);
				}
				dcbm.addElement(member);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	private void initCategory(String categoryName) {
		try {
			List<Category> categories = categoryService.findCategories();
			DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobDetailCategory
					.getModel();
			for (Category category : categories) {
				if (category.getCategoryName().equals(categoryName)) {
					dcbm.setSelectedItem(category);
				}
				dcbm.addElement(category);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	private void initAccount(String accountName) {
		try {
			List<Account> accounts = accountService.findaAccounts();
			DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobDetailAccount
					.getModel();
			for (Account account : accounts) {
				if (account.getName().equals(accountName)) {
					dcbm.setSelectedItem(account);
				}
				dcbm.addElement(account);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		cobDetailAccount = new javax.swing.JComboBox();
		jLabel2 = new javax.swing.JLabel();
		jtfTranTime = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		spnDetailMoney = new javax.swing.JSpinner();
		cobDetailCategory = new javax.swing.JComboBox();
		cobDetailMember = new javax.swing.JComboBox();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jtaNote = new javax.swing.JTextArea();
		btnOK = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("\u8d26\u6237\u540d\u79f0");

		jLabel2.setText("\u4ea4\u6613\u65e5\u671f");

		jLabel3.setText("\u53d1\u751f\u91d1\u989d");

		spnDetailMoney.setModel(new javax.swing.SpinnerNumberModel(Double
				.valueOf(0.0d), Double.valueOf(0.0d), null, Double
				.valueOf(200.0d)));

		jLabel4.setText("\u7c7b\u522b");

		jLabel5.setText("\u5bb6\u5ead\u6210\u5458");

		jLabel6.setText("\u8bf4\u660e");

		jtaNote.setColumns(20);
		jtaNote.setRows(5);
		jScrollPane1.setViewportView(jtaNote);

		btnOK.setText("\u786e\u5b9a");
		btnOK.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnOKActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				19,
																				19,
																				19)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel1)
																						.addComponent(
																								jLabel2)
																						.addComponent(
																								jLabel3)
																						.addComponent(
																								jLabel4)
																						.addComponent(
																								jLabel5)
																						.addComponent(
																								jLabel6))
																		.addGap(
																				38,
																				38,
																				38)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jScrollPane1,
																								0,
																								0,
																								Short.MAX_VALUE)
																						.addGroup(
																								layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING,
																												false)
																										.addComponent(
																												spnDetailMoney)
																										.addComponent(
																												jtfTranTime)
																										.addComponent(
																												cobDetailAccount,
																												0,
																												117,
																												Short.MAX_VALUE)
																										.addComponent(
																												cobDetailCategory,
																												0,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE)
																										.addComponent(
																												cobDetailMember,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												109,
																												javax.swing.GroupLayout.PREFERRED_SIZE))))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				81,
																				81,
																				81)
																		.addComponent(
																				btnOK)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																cobDetailAccount,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(29, 29, 29)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																jtfTranTime,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(39, 39, 39)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel3)
														.addComponent(
																spnDetailMoney,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(41, 41, 41)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																cobDetailCategory,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel4))
										.addGap(41, 41, 41)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel5)
														.addComponent(
																cobDetailMember,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(34, 34, 34)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																69,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel6))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												23, Short.MAX_VALUE)
										.addComponent(btnOK).addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	//点击确定，更新detail表的信息
	private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
		int id = detailVo.getId();
		Account account = (Account)cobDetailAccount.getSelectedItem();
		int accountId = account.getId();
		String accountName = account.getName();
		Date tranTime = StringToDate.getDate(jtfTranTime.getText());
		double happenMoney = (Double)spnDetailMoney.getValue();
		Category category = (Category)cobDetailCategory.getSelectedItem();
		int categoryId = category.getId();
		String categoryName = category.getCategoryName();
		Member member = (Member)cobDetailMember.getSelectedItem();
		int memberId = member.getId();
		String memberName = member.getName();
		String note = jtaNote.getText();
		
		DetailVo detailVo = new DetailVo();
		detailVo.setId(id);
		detailVo.setAccountId(accountId);
		detailVo.setAccountName(accountName);
		detailVo.setTranTime(tranTime);
		detailVo.setHappenMoney(happenMoney);
		detailVo.setCategoryId(categoryId);
		detailVo.setCategoryName(categoryName);
		detailVo.setMemberId(memberId);
		detailVo.setMemberName(memberName);
		detailVo.setNote(note);
		
		try {
			int rowAffected = detailService.updateDetailByDetailVo(detailVo);
			if(rowAffected > 0) {
				JOptionPane.showMessageDialog(this, "修改账务信息成功");
			}else {
				JOptionPane.showMessageDialog(this, "修改账务信息失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new DetailUpdateFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnOK;
	private javax.swing.JComboBox cobDetailAccount;
	private javax.swing.JComboBox cobDetailCategory;
	private javax.swing.JComboBox cobDetailMember;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jtaNote;
	private javax.swing.JTextField jtfTranTime;
	private javax.swing.JSpinner spnDetailMoney;
	// End of variables declaration//GEN-END:variables

}